package com.eastcom.providor.task;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.eastcom.bm.atom.Atom_DataSetMonthStatis;
import com.eastcom.bm.atom.DataSetStatiBean;
import com.eastcom.providor.dao.impl.DataSetDao;

public class DataSetStatisRoutine {

	private DataSetDao dataSetDao;
	
	@Autowired
	public void setDataSetDao(DataSetDao dataSetDao) {
		this.dataSetDao = dataSetDao;
	}

	public DataSetDao getDataSetDao() {
		return dataSetDao;
	}
	
	public void doRoutine(){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(new Date());
		
		Integer year=calendar.get(Calendar.YEAR);
		Integer month=calendar.get(Calendar.MONTH)+1;
		
		Date startDate=getStartTimeOfMonthApart(-1);
		Date endDate=getStartTimeOfMonthApart(0);
		
		try {
			List<DataSetStatiBean> dataSetStatiBeans=dataSetDao.selectDataSetStatiBean(startDate, endDate, getDayCountOfMonth(startDate));
			if(dataSetStatiBeans==null||dataSetStatiBeans.isEmpty())
				return;
			
			List<Atom_DataSetMonthStatis> dataSetMonthStatises=new ArrayList<Atom_DataSetMonthStatis>();
			for(DataSetStatiBean dataSetStatiBean:dataSetStatiBeans){
				Atom_DataSetMonthStatis dataSetMonthStatis=new Atom_DataSetMonthStatis();
				dataSetMonthStatises.add(dataSetMonthStatis);
				
				dataSetMonthStatis.setDataSetId(dataSetStatiBean.getDataSetId());
				dataSetMonthStatis.setDataSetName(dataSetStatiBean.getDataSetName());
				dataSetMonthStatis.setNeType(dataSetStatiBean.getNeType());
				dataSetMonthStatis.setDimensionNum(dataSetStatiBean.getDimensionNum());
				dataSetMonthStatis.setIntegerSum(dataSetStatiBean.getIntegerSum());
				dataSetMonthStatis.setKpiAbnormalNum(dataSetStatiBean.getKpiAbnormalNum());
				dataSetMonthStatis.setKpiAbnormalRatio(dataSetStatiBean.getKpiAbnormalRatio());
				dataSetMonthStatis.setKpiSum(dataSetStatiBean.getKpiSum());
				dataSetMonthStatis.setUnIntegerRatio(dataSetStatiBean.getUnIntegerRatio());
				dataSetMonthStatis.setYear(year);
				dataSetMonthStatis.setMonth(month);
			}
			
			dataSetDao.removeDataSetMonthStatisByParam(year, month);
			dataSetDao.saveDataSetMonthStatis(dataSetMonthStatises);
		} 
		catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Date getStartTimeOfMonthApart(int monthApart){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(new Date());
		
		calendar.add(Calendar.MONTH, monthApart);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		
		return calendar.getTime();
	}
	
	private int getDayCountOfMonth(Date date){
		Calendar calendar=Calendar.getInstance();
		calendar.setTime(date);
		
		calendar.add(Calendar.MONTH, 1);
		calendar.set(Calendar.DAY_OF_MONTH, 1);
		calendar.add(Calendar.DAY_OF_MONTH, -1);
		
		return calendar.get(Calendar.DAY_OF_MONTH);
	}
	

	
}
